<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/2/23
 * Time: 17:07
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Beautytips extends Base
{
    /**
     * 美丽技巧
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $bt_name = trim(input('bt_name'), ' ');
            $where['bt_status'] = ['neq', 3];
            if(isset($bt_name) && $bt_name != ''){
                $where['bt_name'] = ['like', '%' . $bt_name . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::name('beauty_tips')
                ->join('up_beauty_tips_cate', 'bt_cate = btc_id', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('beauty_tips')
                ->join('up_beauty_tips_cate', 'bt_cate = btc_id', 'LEFT')
                ->where($where)
                ->field('bt_id, bt_name, bt_play_number, bt_duration, bt_is_recommend, bt_image, 
                bt_time, btc_name, bt_status')
                ->order('bt_time desc')
                ->page($Nowpage,$limits)
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加美丽技巧
     * @return mixed|\think\response\Json
     */
    public function beautytipsAdd(){
        if(Request::instance()->isPost()){
            $bt_name = trim(input('bt_name'), ' ');
            $bt_image = input('bt_image');
            $bt_video = input('bt_video');
            $bt_status = input('bt_status');
            $bt_is_recommend = input('bt_is_recommend');
            $bt_cate = input('bt_cate');
            
            $insert = [
                'bt_name' => $bt_name,
                'bt_image' => $bt_image,
                'bt_is_recommend' => $bt_is_recommend,
                'bt_status' => $bt_status,
                'bt_cate' => $bt_cate,
                'bt_time' => date('Y-m-d H:i:s')
            ];
            if($bt_video){
                //转码的文件url
                $url = config('qiniu.domain') . $bt_video;//未转码之前的视频url，用于获取视频信息
                $video_info = file_get_contents($url . '?avinfo');
                $video_info = json_decode($video_info,true);
                // 时长
                $time = explode('.', $video_info['streams'][0]['duration']);
                $fen = floor($time[0]/60);
                $miao = floor(($time[0]-$fen*60)%60);
                $insert['bt_duration'] = $fen . ':' . $miao;
                // 大小
                $video_size = $video_info['format']['size'];
                $insert['bt_size'] = $video_size;
                // 转码
                $this->zhuanma($bt_video);
                $insert['bt_video'] = config('qiniu.domain') . 'video_' . $bt_video;
            }
            $res = Db::table('up_beauty_tips')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $cate = Db::table('up_beauty_tips_cate')->where('btc_is_del', 1)->field('btc_id, btc_name')->select();
        return $this->fetch('beautytips/beautytipsadd', ['cate' => $cate]);
    }
    
    /**
     * 编辑美丽技巧
     * @return mixed|\think\response\Json
     */
    public function beautytipsEdit(){
        $bt_id = input('bt_id');
        if(Request::instance()->isPost()){
            $bt_name = trim(input('bt_name'), ' ');
            $bt_image = input('bt_image');
            $bt_video = input('bt_video');
            $bt_status = input('bt_status');
            $bt_is_recommend = input('bt_is_recommend');
            $bt_cate = input('bt_cate');
            
            $update = [
                'bt_name' => $bt_name,
                'bt_is_recommend' => $bt_is_recommend,
                'bt_status' => $bt_status,
                'bt_cate' => $bt_cate,
                'bt_updatetime' => date('Y-m-d H:i:s')
            ];
            if(!empty($bt_image)){
                $update['bt_image'] = $bt_image;
            }
            if($bt_video){
                //转码的文件url
                $url = config('qiniu.domain') . $bt_video;//未转码之前的视频url，用于获取视频信息
                $video_info = file_get_contents($url . '?avinfo');
                $video_info = json_decode($video_info,true);
                // 时长
                $time = explode('.', $video_info['streams'][0]['duration']);
                $fen = floor($time[0]/60);
                $miao = floor(($time[0]-$fen*60)%60);
                $update['bt_duration'] = $fen . ':' . $miao;
                // 大小
                $video_size = $video_info['format']['size'];
                $update['bt_size'] = $video_size;
                // 转码
                $this->zhuanma($bt_video);
                $update['bt_video'] = config('qiniu.domain') . 'video_' . $bt_video;
            }
            $res = Db::table('up_beauty_tips')->where('bt_id', $bt_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $cate = Db::table('up_beauty_tips_cate')->where('btc_is_del', 1)->field('btc_id, btc_name')->select();
        $bt = Db::table('up_beauty_tips')
            ->where('bt_id', $bt_id)
            ->field('bt_name, bt_image, bt_video, bt_is_recommend, bt_status, bt_cate, bt_size')
            ->find();
        return $this->fetch('beautytips/beautytipsedit', ['cate' => $cate, 'bt' => $bt, 'bt_id' => $bt_id]);
    }
    
    /**
     * 美丽技巧详情
     * @return mixed
     */
    public function beautytipsDetail(){
        $bt_id = input('bt_id');
        $detail = Db::table('up_beauty_tips')
            ->join('up_beauty_tips_cate', 'bt_cate = btc_id', 'LEFT')
            ->where('bt_id', $bt_id)
            ->field('bt_name, bt_play_number, bt_video, bt_is_recommend, bt_image, 
                bt_time, btc_name, bt_status')
            ->find();
        return $this->fetch('beautytips/beautytipsdetail', ['detail' => $detail]);
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function beautytipsDown(){
        $bt_id = input('id');
        $res = Db::table('up_beauty_tips')->where('bt_id', $bt_id)->setField('bt_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 100, 'msg' => '下架失败']);
        }
        
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function beautytipsOn(){
        $bt_id = input('id');
        $res = Db::table('up_beauty_tips')->where('bt_id', $bt_id)->setField('bt_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 100, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function beautytipsUnrecommend(){
        $bt_id = input('id');
        $res = Db::table('up_beauty_tips')->where('bt_id', $bt_id)->setField('bt_is_recommend', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function beautytipsRecommend(){
        $bt_id = input('id');
        $res = Db::table('up_beauty_tips')->where('bt_id', $bt_id)->setField('bt_is_recommend', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function beautytipsDel(){
        $bt_id = input('id');
        $res = Db::table('up_beauty_tips')->where('bt_id', $bt_id)->setField('bt_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}